/**
 * $version:  0.1 
 * $Date: 2012-05-22 
 *
 * Copyright (C) 2010-2011 Jawa Software. All rights reserved.
 *
 */

package org.jawa.auth;

import java.io.Serializable;

import org.jawa.user.UserManager;

/**
 * 
 * 验证令牌,用户是否已成功验证。
 */
public class AuthToken implements Serializable {
	private static final long serialVersionUID = 6782533351869406151L;
	private String username;
	private Boolean anonymous;

	/**
	 * Constucts a new AuthToken with the specified username.
	 * The username can be either a simple username or a full JID.
	 *
	 * @param jid the username or bare JID to create an authToken token with.
	 */
	public AuthToken(String username) {
		this.username = username;
	}

	public AuthToken(String username, Boolean anonymous) {
		this.username = username;
		this.anonymous = anonymous;
	}

	/**
	 * Returns the username associated with this AuthToken. A <tt>null</tt> value
	 * means that the authenticated user is anonymous.
	 *
	 * @return the username associated with this AuthToken or null when using an anonymous user.
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Returns true if this AuthToken is the Anonymous auth token.
	 *
	 * @return true if this token is the anonymous AuthToken.
	 */
	public boolean isAnonymous() {
		if (anonymous == null) {
			anonymous = username == null || !UserManager.getInstance().isRegisteredUser(username);
		}
		return anonymous;
	}
}